package com.geek.admin.common.utils;

import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.SQLException;
import java.util.UUID;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author gaozeyu
 */
public class UtilStr {
    private static final Logger log = LoggerFactory.getLogger(UtilStr.class);
    public static final String EMPTY = "";
    public static final String COMMA_EN = ",";
    public static final String DASH = "-";
    public static final String UNDERLINE = "_";
    public static final String DOT = ".";
    public static final String IMAGE_BASE64_PREFIX = "data:image/jpeg;base64,";

    public UtilStr() {
    }

    public static boolean isEmpty(CharSequence str) {
        return str == null || str.length() == 0;
    }

    public static boolean isNotEmpty(CharSequence str) {
        return !isEmpty(str);
    }

    public static String str(CharSequence cs) {
        return null == cs ? null : cs.toString();
    }

    public static String str(Blob blob) {
        if (!UtilObj.isEmpty(blob)) {
            try {
                return str(blob.getBytes(1L, Integer.parseInt(blob.length() + "")));
            } catch (SQLException var2) {
                log.error("读取blob时出错", var2);
            }
        }

        return "";
    }

    public static String str(Clob clob) {
        if (!UtilObj.isEmpty(clob)) {
            try {
                return clob.getSubString(1L, Integer.parseInt(clob.length() + ""));
            } catch (SQLException var2) {
                log.error("读取clob时出错", var2);
            }
        }

        return "";
    }



    public static String str(byte[] bytes) {
        return new String(bytes, StandardCharsets.UTF_8);
    }

    public static String str(byte[] bytes, String charset) {
        return isEmpty(charset) ? str(bytes) : new String(bytes, Charset.forName(charset));
    }

    public static String subSuf(CharSequence string, int fromIndex) {
        return isEmpty(string) ? str(string) : sub(string, fromIndex, string.length());
    }

    public static String sub(CharSequence str, int fromIndex, int toIndex) {
        if (isEmpty(str)) {
            return str(str);
        } else {
            int len = str.length();
            if (fromIndex < 0) {
                fromIndex += len;
                if (fromIndex < 0) {
                    fromIndex = 0;
                }
            } else if (fromIndex > len) {
                fromIndex = len;
            }

            if (toIndex < 0) {
                toIndex += len;
                if (toIndex < 0) {
                    toIndex = len;
                }
            } else if (toIndex > len) {
                toIndex = len;
            }

            if (toIndex < fromIndex) {
                int tmp = fromIndex;
                fromIndex = toIndex;
                toIndex = tmp;
            }

            return fromIndex == toIndex ? "" : str.toString().substring(fromIndex, toIndex);
        }
    }

    public static String lowerFirst(CharSequence str) {
        if (null == str) {
            return null;
        } else {
            if (str.length() > 0) {
                char firstChar = str.charAt(0);
                if (Character.isUpperCase(firstChar)) {
                    return Character.toLowerCase(firstChar) + subSuf(str, 1);
                }
            }

            return str.toString();
        }
    }

    public static String lowerUUID32() {
        return uuid().replaceAll("-", "").toLowerCase();
    }

    public static String uuid() {
        return UUID.randomUUID().toString();
    }

    public static String repeatByLength(CharSequence str, int len) {
        if (isEmpty(str)) {
            return "";
        } else {
            int strLength = str.length();
            if (len == strLength) {
                return str.toString();
            } else if (strLength > len) {
                return sub(str, 0, len);
            } else {
                char[] padding = new char[len];

                for(int i = 0; i < len; ++i) {
                    padding[i] = str.charAt(i % strLength);
                }

                return new String(padding);
            }
        }
    }

    public static String padPre(CharSequence str, int len, CharSequence padStr) {
        if (isEmpty(str)) {
            return "";
        } else {
            int strLength = str.length();
            if (strLength == len) {
                return str.toString();
            } else {
                return strLength > len ? sub(str, 0, len) : repeatByLength(padStr, len - strLength).concat(str.toString());
            }
        }
    }

    public static String encode(String str) {
        try {
            return URLEncoder.encode(str, StandardCharsets.UTF_8.name());
        } catch (Exception var2) {
            log.error("URLEncoder时失败", var2);
            return str;
        }
    }

    public static String decode(String str) {
        try {
            return URLDecoder.decode(str, StandardCharsets.UTF_8.name());
        } catch (Exception var2) {
            log.error("URLDecoder时失败", var2);
            return str;
        }
    }

    public static byte[] bytes(String str) {
        return isEmpty(str) ? null : str.getBytes(StandardCharsets.UTF_8);
    }

    public static byte[] bytes(String str, String charset) {
        if (isEmpty(charset)) {
            return bytes(str);
        } else {
            return isEmpty(str) ? null : str.getBytes(Charset.forName(charset));
        }
    }

    public static String addImageBase64Prefix(String base64) {
        return "data:image/jpeg;base64," + base64;
    }

    public static String removeImageBase64Prefix(String base64WithPrefix) {
        return subSuf(base64WithPrefix, "data:image/jpeg;base64,".length());
    }
}